import ElementUI from 'element-ui';
import i18n from '@/locale';
import { captcha, sendEmail, sendSms } from '@/api/user';
import { EmailSms } from '@/libs/util'

var Public = new Object();

// 手机号验证
Public.phone = function (tel) {
    var phone = tel.trim()
    if(phone){
        return true
    }
    return false
}

// 邮箱验证
Public.email = function (em) {
    if(em.indexOf("@") != -1){
        return true
    }
    return false
}

// 身份证号验证
Public.idCard = function (value) {
    if(value){
        return true
    }
    return false
}

// 密码格式
Public.pwds = function (val) {
    var reg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,20}$/
    return reg.test(val)
}

Public.captcha = function (data, vuethis) {//失败弹框
    var _data = {
        phone: data.phone,
        key_str: data.phone
    };
    captcha(_data).then(res => {
        if (res.data.status_code == 200) {
            data.key_str = res.data.data.captcha_key;
            ElementUI.MessageBox.prompt( i18n.t('header.tishi[6]'), {
                customClass: "captcha",
                showInput: true,
                inputPlaceholder:  i18n.t('header.tishi[6]'),
                closeOnClickModal: false,
                inputPattern: /\S/,
                inputErrorMessage: i18n.t('header.tishi[7]'),
                dangerouslyUseHTMLString: true,
                title: i18n.t('header.tishi[1]'),
                message: "<img src='" + res.data.data.captcha_image_content + "'>",
                beforeClose: (action, instance, done, value) => {
                    if (action === 'confirm') {
                        instance.confirmButtonLoading = true;
                        instance.confirmButtonText =  i18n.t('header.tishi[8]');
                        data.captcha_code = instance.inputValue;
                        
                        if (EmailSms(data.phone)) {
                            data.email = data.phone;
                            delete data.phone;
                            delete data.type;
                            sendEmail(data).then(res => {
                                if (res.data.status_code == 200) {
                                    done();
                                    vuethis.typecaptcha();
                                    instance.confirmButtonLoading = false;
                                    ElementUI.Message({
                                        message:  i18n.t('header.tishi[9]'),
                                        type: 'success'
                                    });
                                } else {
                                    instance.confirmButtonLoading = false;
                                    instance.confirmButtonText =  i18n.t('header.tishi[10]');
                                    ElementUI.Message.error(res.data.message);
                                }
                            }).catch(err => {
                                instance.confirmButtonLoading = false;
                                instance.confirmButtonText =  i18n.t('header.tishi[10]');
                                ElementUI.Message.error(err.msg);
                            })

                        } else {
                            sendSms(data).then(res => {
                                if (res.data.status_code == 200) {
                                    done();
                                    vuethis.typecaptcha();
                                    instance.confirmButtonLoading = false;
                                    ElementUI.Message({
                                        message: i18n.t('header.tishi[9]'),
                                        type: 'success'
                                    });
                                } else {
                                    instance.confirmButtonLoading = false;
                                    instance.confirmButtonText = i18n.t('header.tishi[10]');
                                    ElementUI.Message.error(res.data.message);
                                }
                            }).catch(err => {
                                instance.confirmButtonLoading = false;
                                instance.confirmButtonText = i18n.t('header.tishi[10]');
                                ElementUI.Message.error(err.msg);
                            })
                        };
                    } else {
                        done();
                    }
                }
            }).then(({ value }) => {

            }).catch(() => {
                // ElementUI.Message({
                //     type: 'info',
                //     message: '取消验证'
                // });
            });
        } else {
            this.$message.error(res.data.message);
        }
    }).catch(err => {

    })
};

//身份证号验证
Public.isCardID = function (sId) {
    // var id = /^[1-9][0-9]{5}(19|20)[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|31)|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}([0-9]|x|X)$/;
    if (sId) {
        return true;
    }else {
        return false;
    }
}

Public.toLowFixed = function(num, code, x){
    var obj={
        'btc/usdt':2,
        'eth/usdt':2,
        'etc/usdt':4,
        'bch/usdt':2,
        'eos/usdt':4,
        'ltc/usdt':2,
        'xrp/usdt':5,
        'dash/usdt':2,
        'bsv/usdt':4,
        'ada/usdt':6,
        'trx/usdt':6,
        'xlm/usdt':6,
        'ht/usdt':4,
        'atom/usdt':4,
    }
    var len = obj[code] ? obj[code] : 4
	x ? len = x : ""
	var _num = Number(num).toFixed(len + 1);
	return _num.substr(0, _num.length - 1)
}

// 乘法
Public.Multiplication = function(arg1, arg2) {
	var arg1 = arg1 == '' ? 0 : arg1;
	var arg2 = arg2 == '' ? 0 : arg2;
	var r1, r2, m, n, q;
	if(parseInt(arg1) == arg1) {
		r1 = 0;
	} else {
		r1 = arg1.toString().split(".")[1].length;
	}
	if(parseInt(arg2) == arg2) {
		r2 = 0;
	} else {
		r2 = arg2.toString().split(".")[1].length;
	}
	m = Math.pow(10, r1);
	n = Math.pow(10, r2);
	q = m * n;
	var sum = (((arg1 * m) * (arg2 * n)) / q);
	return sum;
};


export default Public